package com.liuwei.msmall.order.config;

import org.springframework.amqp.core.Binding;
import org.springframework.amqp.core.Queue;
import org.springframework.amqp.core.TopicExchange;
import org.springframework.amqp.rabbit.annotation.EnableRabbit;
import org.springframework.amqp.support.converter.Jackson2JsonMessageConverter;
import org.springframework.amqp.support.converter.MessageConverter;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.HashMap;
import java.util.Map;

/**
 * @Classname MyMqConfig
 * @Description TODO
 * @Date 2020/7/20 15:16
 * @Created by 刘
 */
//使用死信队列来定时处理一些未支付的订单
@EnableRabbit  //开启用于监听消息队列功能
@Configuration
public class MyMqConfig {


    //配置成使用序列化为json的序列化机制
    @Bean
    public MessageConverter getMessageConverter(){
        return new Jackson2JsonMessageConverter();
    }

    //第一次启动服务，并在监听某个队列的时候，rabbitMQ将会自动创建，Binding，Queue，Exchange
    //如果rabbitMq中已经创建了，就不会再次创建
    @Bean
    public Queue orderDelayQueue(){

        //String name, boolean durable, boolean exclusive, boolean autoDelete,
        //@Nullable Map<String, Object> arguments
        Map<String, Object> arguments=new HashMap<>();
        arguments.put("x-dead-letter-exchange","order-event-exchange");//死信交给哪个死信路由
        arguments.put("x-message-ttl",180000);//消息过期时间，默认是毫秒单位
        arguments.put("x-dead-letter-routing-key","order.release.order");//路由键
        Queue queue = new Queue("order.delay.queue",true,false,false,arguments);
        return queue;
    }
    @Bean
    public Queue orderReleaseOrderQueue(){
        Queue queue = new Queue("order.release.order.queue",true,false,false);
        return queue;
    }
    @Bean
    public TopicExchange orderEventExchange(){
        //String name, boolean durable, boolean autoDelete, Map<String, Object> arguments
        TopicExchange topicExchange = new TopicExchange("order-event-exchange",true,false,null);
        return topicExchange;
    }
    @Bean
    public Binding orderCreateBinding(){

        //String destination, DestinationType destinationType, String exchange, String routingKey,
        //@Nullable Map<String, Object> arguments
        Binding binding = new Binding("order.delay.queue", Binding.DestinationType.QUEUE,
                "order-event-exchange","order.create.order",null);

        return binding;
    }
    @Bean
    public Binding orderReleaseBinding(){
        Binding binding = new Binding("order.release.order.queue", Binding.DestinationType.QUEUE,
                "order-event-exchange","order.release.order",null);
        return binding;
    }

    @Bean
    public Queue orderSeckillOrderQueue(){
        Queue queue = new Queue("order.seckill.order.queue",true,false,false);
        return queue;
    }

    @Bean
    public Binding orderSeckillBinding(){
        Binding binding = new Binding("order.seckill.order.queue", Binding.DestinationType.QUEUE,
                "order-event-exchange","order.seckill.order",null);
        return binding;
    }

}
